package com.wanxi.user.service.impl;

import com.wanxi.user.api.ShoppingCartFeign;
import com.wanxi.user.domain.dto.DelShoppingCartDto;
import com.wanxi.user.domain.dto.ShoppingCartDto;
import com.wanxi.user.domain.entity.ShoppingCart;
import com.wanxi.user.domain.query.ShoppingQuery;
import com.wanxi.user.domain.result.UserResult;
import com.wanxi.user.entity.ProductNumber;
import com.wanxi.user.mapper.ShoppingCartMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:Iny
 * @Data：2022/1/10 13:33
 */
@RestController
@RequestMapping("/back/shoppingCart")
public class ShoppingCartServiceImpl implements ShoppingCartFeign {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;


    /**
     *
     * @param productCartDto
     * @return
     */
    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("addCartProduct")
    public UserResult<Long> addCartProduct(ShoppingCartDto productCartDto) {
        //判断商品是否存在购物车中，存在则添加数量
        ShoppingCart byUserIdAndSkuId = shoppingCartMapper.findByUserIdAndSkuId(new ShoppingQuery(productCartDto.getUserId(), productCartDto.getSkuId()));
        if (byUserIdAndSkuId!=null){
            int productNumber = productCartDto.getProductNum()+byUserIdAndSkuId.getProductNum();
            int updateNumber = shoppingCartMapper.updateNumber(new ProductNumber(byUserIdAndSkuId.getUserId(),productNumber));
            if (updateNumber>0){
                return UserResult.success(byUserIdAndSkuId.getId());
            }
        }
        //不存在添加到购物车
        int addProduct = shoppingCartMapper.addProduct(productCartDto);
        if (addProduct>0){
            return UserResult.success((long) productCartDto.getId());
        }
        return UserResult.failed();

    }

    /**
     * 删除购物车商品
     *
     * @param shoppingCartDto 用户id和商品skuId
     * @return
     */
    @ApiOperation(value = "删除购物车商品")
    @ApiImplicitParam(name = "shopping", value = "用户id和商品skuId", dataType = "long", required = true)
    @PutMapping("delCartProduct")
    public UserResult<String> delShoppingCartByUserIdAndSkuId(DelShoppingCartDto shoppingCartDto) {
        int del= shoppingCartMapper.delCartProduct(shoppingCartDto);
        if (del>0){
            return UserResult.success();
        }
        return UserResult.failed();
    }

    /**
     * 清空购物车商品
     *
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value = "清空购物车商品")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "long", required = true)
    @PutMapping("delCartProductAll")
    public UserResult<Boolean> delCartProductAll(long userId) {
        int del = shoppingCartMapper.delByUserId((int)userId);
        if (del > 0){
            return UserResult.success(del > 0);
        }
        return UserResult.failed(del > 0);
    }



    @ApiOperation(value = "查看购物车商品")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "long", required = true)
    @GetMapping("findCartProduct")
    public UserResult<List<ShoppingCart>> findCartProduct(long userId) {
        List<ShoppingCart> skuId =  shoppingCartMapper.findByUserId((int)userId);
        if (skuId!=null){
            return UserResult.success(skuId);
        }
        return UserResult.failed();
    }

}
